package com.sikao.app.utils;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.myinterface.Callback;

import java.util.List;

/**
 * Created by Administrator on 2016/9/1.
 */
public class ListViewShow <T>{

	private Context context;
	private List<T> beans;
	private View noData,noNetwork;
	private ListView listView;
	private Callback callback;

	public ListViewShow(Context context, List<T> beans, ListView listView, Callback callback) {
		this.context = context;
		this.beans = beans;
		this.listView = listView;
		this.callback = callback;
	}



	public void showNoData(String text){
		if (noNetwork != null){
			noNetwork.setVisibility(View.GONE);
		}
		//清除数据，数据不为空emptyView不会显示
		if (beans.size() != 0) {
			beans.clear();
			BaseAdapter adapter = (BaseAdapter) listView.getAdapter();
			adapter.notifyDataSetChanged();

		}
		if (noData == null){
			noData = View.inflate(context, R.layout.content_empty,null);
			((ViewGroup) listView.getParent()).addView(noData);
			listView.setEmptyView(noData);
			noData.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					callback.notice(2);
				}
			});
			TextView textView = (TextView) noData.findViewById(R.id.tv_nodata);
			textView.setText(text);
		}else{
			noData.setVisibility(View.VISIBLE);
		}
	}

	public void showNoNetwork(){
		if (noData != null){
			noData.setVisibility(View.GONE);
		}
		//清除数据，数据不为空emptyView不会显示
		if (beans.size() != 0) {
			beans.clear();
			BaseAdapter adapter = (BaseAdapter) listView.getAdapter();
			adapter.notifyDataSetChanged();
		}
		if (noNetwork == null){
			noNetwork = View.inflate(context, R.layout.no_network,null);
			((ViewGroup) listView.getParent()).addView(noNetwork);
			listView.setEmptyView(noNetwork);
			Button button = (Button) noNetwork.findViewById(R.id.btn_request);
			button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					callback.notice(1);
				}
			});
		}else{
			noNetwork.setVisibility(View.VISIBLE);
		}
	}
}
